package com.syzg.common.model;

import com.syzg.common.config.SysProperties;
import com.syzg.core.exception.ServiceException;
import com.syzg.core.page.PageResult;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class WebObj
{
  private WebInfo webInfo;
  private String[] selectedRow;
  private int pageNo;
  private PageResult pageResult;
  private String opType;
  private int pageSize;
  private Object obj;
  private Map<String, String> searchValue;

  public WebObj()
  {
    this.webInfo = new WebInfo();
    this.searchValue = new HashMap();
  }

  public Map<String, String> getSearchValue() {
    return this.searchValue;
  }

  public void setSearchValue(Map<String, String> searchValue) {
    this.searchValue = searchValue;
  }

  public int getPageSize() {
    HttpSession session = ((ServletRequestAttributes)
      RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    if ((session != null) && (session.getAttribute("CUSTOM_PAGE_SIZE") != null)) {
      this.pageSize = Integer.valueOf((String)session.getAttribute("CUSTOM_PAGE_SIZE")).intValue();
      if (this.pageSize == 0)
        this.pageSize = SysProperties.PAGESIZE;
    } else {
      this.pageSize = SysProperties.PAGESIZE;
    }
    return this.pageSize;
  }

  public void handleException(Exception e) {
    if ((e instanceof ServiceException)) {
      e.printStackTrace();
      addInfo(e.getMessage());
    } else {
      e.printStackTrace();
      addError(e.getMessage());
    }
    setActionStatus("failure");
  }

  public boolean isSuccess() {
    String error = getWebInfo().getActionStatus();
    boolean isSuccess = true;
    if ((error != null) && (error.equals("failure")))
      isSuccess = false;
    return isSuccess;
  }

  public Long[] getSelectedRowLong() {
    if ((this.selectedRow != null) && (this.selectedRow.length > 0))
    {
      Long[] reurnValue = new Long[this.selectedRow.length];
      for (int i = 0; i < this.selectedRow.length; i++) {
        reurnValue[i] = Long.valueOf(Long.parseLong(this.selectedRow[i]));
      }
      return reurnValue;
    }

    return null;
  }

  public void addError(String error)
  {
    this.webInfo.addError(error);
  }

  public void addInfo(String info) {
    this.webInfo.addInfo(info);
  }

  public void setActionStatus(String actionStatus) {
    this.webInfo.setActionStatus(actionStatus);
  }

  public PageResult getPageResult() {
    return this.pageResult;
  }

  public void setPageResult(PageResult pageResult) {
    this.pageResult = pageResult;
  }

  public WebInfo getWebInfo() {
    return this.webInfo;
  }

  public void setWebInfo(WebInfo webInfo) {
    this.webInfo = webInfo;
  }

  public int getPageNo() {
    return this.pageNo;
  }

  public void setPageNo(int pageNo) {
    this.pageNo = pageNo;
  }

  public String[] getSelectedRow() {
    return this.selectedRow;
  }

  public void setSelectedRow(String[] selectedRow) {
    this.selectedRow = selectedRow;
  }

  public String getOpType() {
    return this.opType;
  }

  public void setOpType(String opType) {
    this.opType = opType;
  }

  public Object getObj() {
    return this.obj;
  }

  public void setObj(Object obj) {
    this.obj = obj;
  }
}